package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.*;
import cn.itcast.utils.BeanMapUtils;
import cn.itcast.utils.DownloadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.mchange.v2.beans.BeansUtils;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.poi.ss.formula.functions.FinanceFunction;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("cargo/finance")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;

    @Reference
    private InvoiceService invoiceService;

    @Reference
    private ShippingOrderService  shippingOrderService;

    @Reference
    private ExportService exportService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportProductService exportProductService;


    @RequestMapping(value = "/list",name = "显示财务列表")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        FinanceExample example = new FinanceExample();
        example.createCriteria().andCompanyIdEqualTo(getCompanyId());
        PageInfo<Finance> pageInfo = financeService.findAll(example, page, pageSize);
        request.setAttribute("page", pageInfo);
        return "cargo/finance/finance-list";
    }

    @RequestMapping(value = "toAdd", name = "进入添加财务单页面")
    public String toAdd(String id) {
        //添加财务报运单,只能对已经支付的发票生成财务报运单
        //需要将发票数据回显回去
        //需要到委托表里找装运港、目的港、收货人
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder", shippingOrder);
        /*InvoiceExample example = new InvoiceExample();
        example.createCriteria().andInvoiceIdEqualTo(id);*/
        Invoice invoice = invoiceService.findById(id);
        request.setAttribute("invoice", invoice);
//        request.setAttribute("invoiceMoney", Integer.parseInt(invoice.getInvoiceMoney()));
        request.setAttribute("invoiceMoney", 100);  //假数据

        return "cargo/finance/finance-add";
    }

    @RequestMapping(value = "edit", name = "保存财务单")
    public String edit(Finance finance) {
        System.out.println(finance);
        finance.setCompanyId(getCompanyId());
        financeService.save(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @Autowired
    private DownloadUtil downloadUtil;

    @RequestMapping(value = "printTemplate", name = "打印财务报运单")
    public void printTemplate(String financeId) throws Exception{
        //查询制单日期、发票号、export、exportProductList

        Finance finance = financeService.findById(financeId);
        Packing packing = packingService.findById(financeId);

        Export export = exportService.findById(packing.getExportIds().split(",")[0]);
        List<ExportProduct> exportProductList = exportProductService.findByExportId(export.getId());

        //读取模版
        String realPath = session.getServletContext().getRealPath("template/export_finance.jasper");
        FileInputStream inputStream = new FileInputStream(realPath);

        //Map
        Map<String, Object> parameters = BeanMapUtils.beanToMap(export);
        parameters.put("inputTime", finance.getInputTime());
        parameters.put("invoiceId", finance.getInvoiceId());

        //集合
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(exportProductList);

        //填充
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, dataSource);

        //生成字节数组
        byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
        //下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        byteArrayOutputStream.write(bytes);

        downloadUtil.download(byteArrayOutputStream, response, "财务报运单.pdf");
    }



}
